<template>
    <div class="notice_modal">
        <div class="notice_modal_title">{{title}}</div>
        <div class="notice_modal_bts">
            <a-icon class="user_close" @click="$emit('close')" type="close" />
        </div>
        <div class="notice_modal_hedaer">
        <frame name="_blank"></frame>
        </div>
        <div class="notice_modal_inner">
            <a-list>
                <RecycleScroller
                    v-infinite-scroll="handleInfiniteOnLoad"
                    style="height: 540px"
                    :items="data"
                    :item-size="90"
                    key-field="notice_id"
                    :infinite-scroll-disabled="busy"
                    :infinite-scroll-distance="10"
                >
                <a-list-item slot-scope="{ item }">
                    <div class="inner_item">
                        <span class="item_icon iconfont" :class="iconClass"></span>
                        <div class="item_information">
                            <div class="item_message">
                                <span style="color: #999">{{title.replace('消息', '')}}：</span>
                                <span class="item_msg" @click="() => handleSpanClick(item.msg)" v-html="item.msg" style="color: #000"></span>
                            </div>
                            <div class="item_date">
                                <span style="color: #999">{{item.createTime}}</span>
                            </div>
                            <a-divider dashed />
                        </div>
                    </div>
                </a-list-item>
                </RecycleScroller>
                <a-spin v-if="loading" class="demo-loading" />
            </a-list>
        </div>

        <notice-work-modal v-if="noticeWorkVisible"
            :visible="noticeWorkVisible"
            :worksheetId="worksheetId"
            :rowId="rowId"
            :taskId="taskId"
            :noticeRoute="noticeRoute"
            @cancel="noticeWorkVisible = false"
        ></notice-work-modal>
    </div>
</template>

<script>
import infiniteScroll from 'vue-infinite-scroll';
import { RecycleScroller } from 'vue-virtual-scroller';
import 'vue-virtual-scroller/dist/vue-virtual-scroller.css';
import { getNoticeList } from "@/api/app"
import NoticeWorkModal from './noticeWorkModal.vue';
export default {
    directives: { infiniteScroll },
    components: {
        RecycleScroller,
        NoticeWorkModal,
    },
    data(){
        return {
            data: [],
            loading: false,
            busy: false,
            size: 7,
            current: 1,
            total: 0,
            noticeWorkVisible: false,
            worksheetId: '',
            noticeRoute: '',
            rowId: false,
            taskId: false,
        }
    },
    props:{
        noticeType: {
            type: Number,
            default: 0
        },
        visible: Boolean,

    },
    watch:{
        async noticeType(){
            this.loading = true
            this.current = 1
            let noticeList = await getNoticeList({
                NoticeType: this.noticeType,
                Size: this.size,
                Current: this.current,
            })
            this.loading = false
            this.data = noticeList.records || []
            this.total = noticeList.total
        }
    },
    methods:{
        handleSpanClick(msg){
            let re = /href=['|"]\/(.*)['|"]\s*target=/
            if(re.test(msg)){
                let route = re.exec(msg)[1]
                let [noticeRoute, id, rowId] = route.split('/')
                this.noticeRoute = noticeRoute
                if(noticeRoute === 'workflowInstance'){
                    this.noticeWorkVisible = true
                    this.worksheetId = id
                    this.rowId = rowId
                }
                else if (noticeRoute === 'workflowApprove'){
                    this.noticeWorkVisible = true
                    this.taskId = id
                }
            }
        },
        async handleInfiniteOnLoad(){
            const data = this.data;
            if(data.length >= this.total) return
            this.loading = true;
            this.current++
            let noticeList = await getNoticeList({
                NoticeType: this.noticeType,
                Size: this.size,
                Current: this.current,
            })
            
            this.data = [...data, ...noticeList.records]
            this.loading = false;
        },
    },
    async mounted(){
        this.loading = true
        let noticeList = await getNoticeList({
            NoticeType: this.noticeType,
            Size: this.size,
            Current: this.current,
        })
        this.loading = false
        this.data = noticeList.records || []
        this.total = noticeList.total
    },
    computed:{
        title(){
            let title = '消息'
            switch(this.noticeType){
                case 0: title = '系统消息'
                    break;
                case 1: title = '工作流消息'
                    break;
                case 2: title = '应用消息'
                    break;
            }
            return title
        },
        iconClass(){
            let iconClass = ''
            switch(this.noticeType){
                case 0: iconClass = 'system cgsIcon-bell'
                    break;
                case 1: iconClass = 'workflow cgsIcon-workflow'
                    break;
                case 2: iconClass = 'app cgsIcon-appyingyong'
                    break;
            }
            return iconClass
        }
    }
}
</script>

<style lang="less">
.notice_modal{
    position: fixed;
    right: 80px;
    bottom: 10px;
    width: 800px;
    height: 600px;
    display: flex;
    flex-direction: column;
    border-radius: 5px;
    overflow: hidden;
    background-color: #fff;
    z-index: 16;
    transition: all .3s;
        box-shadow: 0 3px 10px 2px rgb(0 0 0 / 16%), 0 5px 20px 4px rgb(0 0 0 / 10%);

    .notice_modal_title{
        position: absolute;
        left: 15px;
        top: 15px;
        font-size: 16px;
        font-weight: bold;
    }
    .notice_modal_bts{
        position: absolute;
        right: 20px;
        top: 12px;
        font-size: 16px;
    }
    .notice_modal_hedaer{
        height: 50px;
        border-bottom: 1px solid #e0e0e0;
        background: hsla(0,0%,100%,.2)!important;
    }

    .notice_modal_inner{
        position: absolute;
        width: 100%;
        top: 50px;
        bottom: 0px;
        padding: 10px;

        .ant-list-item{
            padding: 0;
        }

        .inner_item{
            display: flex;
            width: 100%;
            align-items: center;

            .item_icon{
                width: 32px;
                height: 32px;
                line-height: 32px;
                text-align: center;
                border-radius: 50%;
                flex-shrink: 0;
                color: #fff;
                font-size: 16px;
                margin-right: 10px;
            }

            .item_information{
                flex: 1 1 auto;
                .ant-divider-horizontal{
                    margin: 5px 0
                }

                .item_msg{
                    cursor: pointer;
                    a{
                        pointer-events: none;
                    }
                }
            }
            
        }
    }

    .demo-loading {
        position: absolute;
        bottom: 40px;
        width: 100%;
        text-align: center;
    }
}

</style>